import axios from 'axios' // 引入axios
import { Toast } from 'vant'
const axios2 = axios.create({
  baseURL: 'http://kumanxuan1.f3322.net:8001',
  timeout: 5000
})
axios2.interceptors.request.use(function (config) {
  const token = localStorage.getItem('token')
  if (token) {
    config.headers['X-Nideshop-Token'] = token
  }
  // 添加loading
  // 因为loading 会影响list组件的使用 所有添加白名单
  // 以后需要用到loading 的时候  就往白名单加路径
  const whileList = ['/goods/list']
  if (!whileList.includes(config.url)) {
    Toast.loading({
      duration: 0, // 持续展示 toast
      forbidClick: true,
      message: '加载中...'
    })
  }
  return config
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error)
})
axios2.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  if (response.data && response.data.errno !== 0) {
    Toast(response.data.errmsg)
  }
  // 结束loading
  Toast.clear()
  return response.data
}, function (error) {
  return Promise.reject(error)
})
export default ({ url, method = 'get', params = {}, data = {}, headers = {}, transformRequest }) => {
  return axios2({
    url: url,
    method: method,
    params: params,
    data: data,
    headers: headers,
    transformRequest
  })
}
